﻿using Shop.Collect;
using Shop.ExtendService;
using Shop.Model.AdvertPlace;
using Shop.Model.DB;
using Shop.RemoteModel;
using Shop.RemoteModel.AdvertPlace.Model;
using Shop.Service.Helper;
using Shop.Service.Interface;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Shop.Service.lmpl
{
    internal class AdvertPlaceService : IAdvertPlaceService
    {
        private static readonly string _AdvertPlaceType = "AdvertPlaceType";
        private readonly IAdvertPlaceCollect _Service;
        private readonly IAdvertCollect _Advert;
        private readonly IDictItemService _DictItem;
        private readonly IFileService _FileService;

        public AdvertPlaceService ( IAdvertPlaceCollect service,
            IDictItemService dictItem,
            IFileService file,
            IAdvertCollect advert )
        {
            this._FileService = file;
            this._DictItem = dictItem;
            this._Advert = advert;
            this._Service = service;
        }

        public long Add ( AdvertPlaceAdd add )
        {
            PlaceAdd data = add.ConvertMap<AdvertPlaceAdd, PlaceAdd>();
            data.ImgShow = add.ImgSet.GetImgShow(data.ImgNum);
            long id = this._Service.Add(data);
            if ( add.FileId.HasValue )
            {
                this._FileService.Save(add.FileId.Value, id);
            }
            return id;
        }
        public AdvertPlaceItem[] GetItems ( AdvertPlaceGetParam query )
        {
            return this._Service.Gets<AdvertPlaceItem>(query);
        }

        public void Delete ( long id )
        {
            DBAdvertPlace place = this._Service.Get(id);
            this._Service.Delete(place);
            if ( place.ExampleImg.IsNotNull() )
            {
                this._FileService.Drop(place.ExampleImg);
            }
        }
        public AdvertPlaceConfig GetPlaceConfig ( long id )
        {
            PlaceConfig place = this._Service.Get<PlaceConfig>(id);
            return place.ConvertMap<PlaceConfig, AdvertPlaceConfig>();
        }

        public AdvertPlace Get ( long id )
        {
            DBAdvertPlace place = this._Service.Get(id);
            return place.ConvertMap<DBAdvertPlace, AdvertPlace>();
        }
        public Dictionary<long, string> GetExampleImg ( string[] placeType )
        {
            return this._Service.GetExampleImg(placeType);
        }
        public AdvertPlaceDto[] Gets ( AdvertPlaceStatus? status )
        {
            DBAdvertPlace[] list = this._Service.Gets<DBAdvertPlace>(status);
            if ( list.IsNull() )
            {
                return Array.Empty<AdvertPlaceDto>();
            }
            return this._Format(list);
        }
        public PagingResult<AdvertPlaceDto> Query ( AdvertPlaceQueryParam query, IBasicPage paging )
        {
            DBAdvertPlace[] list = this._Service.Query<DBAdvertPlace>(query, paging, out int count);
            if ( list.IsNull() )
            {
                return new PagingResult<AdvertPlaceDto>(count);
            }
            return new PagingResult<AdvertPlaceDto>(this._Format(list), count);
        }
        private AdvertPlaceDto[] _Format ( DBAdvertPlace[] list )
        {
            Dictionary<long, int> adverts = this._Advert.GetAdvertNum(list.ConvertAll(c => c.Id));
            AdvertPlaceDto[] dtos = list.ConvertMap<DBAdvertPlace, AdvertPlaceDto>();
            Dictionary<string, string> placeType = this._DictItem.GetText(_AdvertPlaceType, dtos.Distinct(a => a.PlaceType));
            dtos.ForEach(c =>
            {
                c.AdvertNum = adverts.GetValueOrDefault(c.Id);
                c.PlaceTypeName = placeType.GetValueOrDefault(c.PlaceType);
            });
            return dtos;
        }
        public void Enable ( long id )
        {
            DBAdvertPlace place = this._Service.Get(id);
            this._Service.SetStatus(place, AdvertPlaceStatus.启用);
        }
        public void Stop ( long id )
        {
            DBAdvertPlace place = this._Service.Get(id);
            this._Service.SetStatus(place, AdvertPlaceStatus.停用);
        }
        public bool Set ( long id, AdvertPlaceSet set )
        {
            DBAdvertPlace place = this._Service.Get(id);
            PlaceSet dto = set.ConvertMap<AdvertPlaceSet, PlaceSet>();
            dto.ImgShow = set.ImgSet.GetImgShow(dto.ImgNum);
            string img = place.ExampleImg;
            if ( this._Service.Set(place, dto) )
            {
                this._FileService.Sync(set.FileId, id, img);
                return true;
            }
            return false;
        }
    }
}
